//
//  HLVisitorView.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/15.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLVisitorView.h"
#import <Masonry.h>

@interface HLVisitorView ()

@property (nonatomic,strong) UIImageView *houseView;
@property (nonatomic,strong) UIImageView *lagerView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIButton *loginBtn;
@property (nonatomic,strong) UIButton *registerBtn;
@property (nonatomic,strong) UIImageView *coverView;

@end

@implementation HLVisitorView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        //给子控件设置自动布局
        [self setSubviewsLayoutConstraint];
        //设置背景颜色
        self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1];
        
    }
    return self;
}

//添加子控件的自动布局
- (void)setSubviewsLayoutConstraint {
    //添加子控件
    [self addSubview:self.lagerView];
    [self addSubview:self.coverView];
    [self addSubview:self.houseView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.loginBtn];
    [self addSubview:self.registerBtn];
    //遍历子控件,取消子控件的自适应布局
    for (UIView *subview in self.subviews) {
        subview.translatesAutoresizingMaskIntoConstraints = NO;
    }
    //添加约束
    //房子视图
    [self.houseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).with.offset(-40);
    }];
    //大图
    [self.lagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.houseView.mas_centerX);
        make.centerY.equalTo(self.houseView.mas_centerY);
    }];
    //文本
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.lagerView.mas_centerX);
        make.top.equalTo(self.lagerView.mas_bottom).with.offset(15);
        make.width.equalTo(@220);
        make.height.equalTo(@45);
    }];
    //登陆按钮
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(15);
        make.height.equalTo(@40);
        make.width.equalTo(self.registerBtn.mas_width);
    }];
    //注册按钮
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.titleLabel.mas_right);
        make.top.equalTo(self.loginBtn.mas_top);
        make.left.equalTo(self.loginBtn.mas_right).with.offset(20);
        make.height.equalTo(self.loginBtn.mas_height);
    }];
    //遮罩
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.lagerView.mas_centerX);
        make.width.equalTo(self.mas_width);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.titleLabel.mas_bottom);
    }];
    
    //添加登录按钮点击事件
    [self.loginBtn addTarget:self action:@selector(clickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    //添加注册按钮点击事件
    [self.registerBtn addTarget:self action:@selector(clickRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
}

//登录
- (void)clickLoginBtn {
    //先判断代理方法是否有响应
    if ([self.delegate respondsToSelector:@selector(visitorViewUserLogin)]) {
        //执行代理方法
        [self.delegate visitorViewUserLogin];
    }
}

//注册
- (void)clickRegisterBtn {
    //先判断代理方法是否有响应
    if ([self.delegate respondsToSelector:@selector(visitorViewUserRegister)]) {
        //执行代理方法
        [self.delegate visitorViewUserRegister];
    }
}

//设置大图图片和文字
- (void)setVisitorTitle:(NSString *)title withLagerImageName:(NSString *)imageName {
    //根据传入的图片是否有值来修改
    if (imageName == nil) {
        //添加动画
        [self startAnimation];
    }else {
        self.titleLabel.text = title;
        self.lagerView.image = [UIImage imageNamed:imageName];
        //隐藏房子视图和遮罩视图
        self.houseView.hidden = YES;
        self.coverView.hidden = YES;
    }
}

//设首页的大图添加动画
- (void)startAnimation {
    //创建基本动画
    CABasicAnimation *baseAnm = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    //设置动画路劲
    baseAnm.toValue = @(M_PI * 2);
    //设置重复次数
    baseAnm.repeatCount = MAXFLOAT;
    //设值时间
    baseAnm.duration = 10;
    //设置当动画完成或者视图处于非活跃状态不移除动画
    baseAnm.removedOnCompletion = NO;
    //添加动画
    [self.lagerView.layer addAnimation:baseAnm forKey:nil];
}


#pragma mark - 懒加载子控件
//房子
- (UIImageView *)houseView {
    if (_houseView == nil) {
        _houseView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _houseView;
}
//大图
- (UIImageView *)lagerView {
    if (_lagerView == nil) {
        _lagerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _lagerView;
}
//文本
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithTitle:@"关注一些人，回这里看看有什么惊喜" withTitleFont:14 withTitleColor:[UIColor grayColor]];
    }
    return _titleLabel;
}
//登录按钮
- (UIButton *)loginBtn {
    if (_loginBtn == nil) {
        _loginBtn = [[UIButton alloc] init];
        //设置文字
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        //设置文字颜色
        [_loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置字体大小
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        //设置背景图片
        [_loginBtn setBackgroundImage:[[UIImage imageNamed:@"common_button_white_disable"] stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
    }
    return _loginBtn;
}
//注册按钮
- (UIButton *)registerBtn {
    if (_registerBtn == nil) {
        _registerBtn = [[UIButton alloc] init];
        //设置文字
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        //设置文字颜色
        [_registerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        //设置字体大小
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        //设置背景图片
        [_registerBtn setBackgroundImage:[[UIImage imageNamed:@"common_button_white_disable"] stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
    }
    return _registerBtn;
}
//遮罩
- (UIImageView *)coverView {
    if (_coverView == nil) {
        _coverView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _coverView;
}

@end
